import { CHANGE_INPUT_VALUE, BUTTON_CLICK, DELETE_ITEM, INIT_ITEM } from './actionType'
const defaultState = {
  inputValue: '123',
  list: [1, 2]
}
// 导出默认的数据
// reducer 可以接受state 但是不能修改state
export default (state = defaultState, action) => {
  if (action.type === CHANGE_INPUT_VALUE) {
    console.log(action)
    const newState = JSON.parse(JSON.stringify(state))
    newState.inputValue = action.value
    return newState
  }

  if (action.type === BUTTON_CLICK) {
    console.log(action)
    const newState = JSON.parse(JSON.stringify(state))
    newState.list.push(state.inputValue)
    newState.inputValue = ''
    console.log(newState)
    return newState
  }
  if (action.type === DELETE_ITEM) {
    console.log(action)
    const newState = JSON.parse(JSON.stringify(state))
    newState.list.splice(action.index, 1)
    console.log(newState)
    return newState
  }

  // 初始化数据
  if (action.type === INIT_ITEM) {
    const newState = JSON.parse(JSON.stringify(state))
    newState.list = action.list
    return newState
  }

  return state
}